package cn.com.cpic.insurance.rulecheck.web;

import java.io.IOException;
import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.cpic.insurance.rulecheck.consumer.BooleanResult;
import cn.com.cpic.insurance.rulecheck.core.ConditionHelper;
import cn.com.cpic.insurance.rulecheck.core.Context;
import cn.com.cpic.insurance.rulecheck.util.ContextUtils;
import cn.com.cpic.insurance.rulecheck.util.ResultUtils;

import com.opensymphony.xwork2.ActionSupport;

public class SampleAction extends ActionSupport {

	private static final long serialVersionUID = 3066746425926319387L;

	private ConditionHelper helper;

	private String rtParam;

	private String ruleName;

	public void check() throws Exception {
		Context context = ContextUtils.getContext(rtParam);
		helper.init(context);
		BooleanResult result = helper.consume();
		ajaxData(ResultUtils.booleanResultToJson(result));
		helper.clean();
	}

	public void getRule() throws Exception {
		Context context = ContextUtils.getContext(rtParam);
		helper.init(context);
		ajaxData(helper.getRule(ruleName).toString());
		helper.clean();
	}

	private void ajaxData(String data) throws IOException {
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		out.print(data);
		out.flush();
	}

	@Autowired
	public void setHelper(ConditionHelper helper) {
		this.helper = helper;
	}

	public String getRtParam() {
		return rtParam;
	}

	public void setRtParam(String rtParam) {
		this.rtParam = rtParam;
	}

	public String getRuleName() {
		return ruleName;
	}

	public void setRuleName(String ruleName) {
		this.ruleName = ruleName;
	}
}
